import {
  CoolController,
  BaseController,
  CoolTag,
  TagTypes,
  CoolCommException,
} from '@cool-midway/core';
import { DeviceDeviceEntity } from '../../entity/device';
import { DeviceDeviceService } from '../../service/device';
import { Body, Inject, Post } from '@midwayjs/core';
/**
 * 设备信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: DeviceDeviceEntity,
  service: DeviceDeviceService,
})
export class AppDeviceDeviceController extends BaseController {
  @Inject()
  deviceDeviceService: DeviceDeviceService;

  // 绑定设备
  @Post('/bind')
  async bind(@Body() params) {
    return this.ok(await this.deviceDeviceService.bind(params));
  }

  // 获取当前用户绑定的设备
  @Post('/getBindDevice')
  async getBindDevice(params) {
    return this.ok(await this.deviceDeviceService.getList(params));
  }

  @Post('/sendCommand', { summary: '向指定设备发送指令' })
  async sendCommand(@Body() body: any) {
    console.log('到这里吗啊', body);

    return this.ok(await this.deviceDeviceService.sendCommand(body));
  }

  // 通过设备id获取设备信息
  @Post('/getDeviceInfo', { summary: '通过设备id获取设备信息' })
  async getDeviceInfo(@Body() body: any) {
    return this.ok(await this.deviceDeviceService.getDeviceInfo(body));
  }

  // 设置报警配置
  @Post('/setAlertConfig', { summary: '设置报警配置' })
  async setAlertConfig(@Body() params) {
    // 判断手机号是否为空
    if (params.alertPhone?.length > 0) {
      // 手机号传进来是一个数组，需要转成字符串
      params.alertPhone = params.alertPhone.join(',');
    }

    // 这里可以实现告警配置逻辑
    // 例如：保存到配置表，设置监控规则等
    return this.ok(await this.deviceDeviceService.setAlertConfig(params));
  }

  // 通过设备id获取预警配置
  @Post('/getAlertConfig', { summary: '获取预警配置' })
  async getAlertConfig(@Body() params) {
    return this.ok(await this.deviceDeviceService.getAlertConfig(params));
  }

  // 发送短信
  @Post('/sendSms', { summary: '发送短信' })
  async sendSms(@Body() params) {
    return this.ok(await this.deviceDeviceService.sendSms(params));
  }

  // 获取设备所在地的天气
  @Post('/getDeviceWeather', { summary: '获取设备所在地的天气' })
  async getDeviceWeather(@Body() params) {
    return this.ok(await this.deviceDeviceService.getDeviceWeather(params));
  }
}
